package com.duobaoyu.dby.supply.db.entity.config;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.duobaoyu.dby.common.mybatis.base.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 预警配置表
 * </p>
 *
 * @author dby
 * @since 2023-03-23
 */
@Getter
@Setter
@TableName("sc_pre_warning_config")
public class PreWarningConfig extends BaseEntity implements Comparable<PreWarningConfig> {

	private static final long serialVersionUID = 1L;

	/**
	 * 主键
	 */
	private Long id;

	/**
	 * 比较符号: >
	 */
	private String compareSymbol;

	/**
	 * 限制时间
	 */
	private Integer limitTimeNum;

	/**
	 * 单位:1-天 2-小时
	 */
	private Integer unit;

	/**
	 * 接收手机号,逗号分隔
	 */
	private String mobile;

	/**
	 * 支持查询业务场景: 1-订单查询 2-虚拟查询 3-售后查询
	 */
	private Integer queryBizScene;

	/**
	 * 预警分类
	 * //@see om.duobaoyu.dby.channel.adapter.enums.biz.config.PreWarningConfigTypeEnum
	 */
	private Integer type;

	/**
	 * 前端分组名称
	 */
	private String groupName;

	/**
	 * 前端分组编码
	 */
	private Integer groupCode;

	/**
	 * 预警配置项描述
	 */
	private String itemDescribe;

	/**
	 * 版本号-用作乐观锁
	 */
	private Integer version;

	/**
	 * 是否删除 0-否 1-是
	 */
	@TableLogic(value = "0", delval = "1")
	private Integer delFlag;

	private long toTimeMillis() {
		TimeUnit timeUnit;
		if (this.getUnit() == 1) {
			timeUnit = TimeUnit.DAYS;
		} else {
			timeUnit = TimeUnit.HOURS;
		}
		return timeUnit.toMillis(Optional.ofNullable(this.limitTimeNum).orElse(0));
	}


	@Override
	public int compareTo(@NotNull PreWarningConfig o) {
		if (Objects.isNull(o)) {
			return 1;
		}
		if (this == o) {
			return 0;
		}
		return Long.compare(this.toTimeMillis(), o.toTimeMillis());
	}
}
